/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package data;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author Ashish
 *
 */
public class DataSet {
	private ArrayList<FeatureVector> dataSet;
	private String category;
	private int[][] dataSetMatrix;
	/**
	 * @param dataSet
	 * @param category
	 */
	public DataSet() {
		this.dataSet = new ArrayList<FeatureVector>();
		this.category = null;
		this.dataSetMatrix = null;
	}

	/**
	 * @param dataSet the dataSet to set
	 */
	public void setDataSet(ImageSet imageSet) {
		Iterator<Image> images = imageSet.getImages().iterator();
		while(images.hasNext()){
			Iterator<FeatureVector> descriptors = images.next().getDescriptors().iterator();
			while(descriptors.hasNext()){
				this.dataSet.add(descriptors.next());
			}
		}
	}	

	/**
	 * @param dataSet the dataSet to set
	 */
	public void setDataSet(FeatureVector featureVector) {
		this.dataSet.add(featureVector);
	}

	/**
	 * @return the dataSet
	 */
	public ArrayList<FeatureVector> getDataSet() {
		return dataSet;
	}

	/**
	 * @param category the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	/**
	 * @return the category
	 */
	public String getCategory() {
		return category;
	}



	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		Iterator<FeatureVector> itr = this.dataSet.iterator();
		while(itr.hasNext()){
			result.append(itr.next().toString());
			result.append("\n");
		}
		return result.toString();
	}



	/**
	 * @param imageSet
	 * @throws IOException 
	 */
	public void read(ImageSet imageSet) throws IOException {
		Iterator<String> itr = imageSet.imageName.iterator();
		while(itr.hasNext()){
			String keypointFilename = imageSet.resourceLocation + itr.next() + ".key";
			File file = new File(keypointFilename);
			Reader fr = new FileReader(file);
			StreamTokenizer st = new StreamTokenizer(fr);
			st.parseNumbers();
			st.nextToken();
			int numFeatureVectors = (int) st.nval;
			st.nextToken();
			int numDescriptorDimensions = (int) st.nval;
			for(int i = 0; i < numFeatureVectors; i++){
				FeatureVector fv = new FeatureVector(numDescriptorDimensions);
				for(int j=0;j<4;j++){
					st.nextToken();
				}
				for(int j=0;j<numDescriptorDimensions;j++){
					st.nextToken();
					int feature = (int) st.nval;
					fv.setFeatureVector(feature);
				}
				this.dataSet.add(fv);
			}
		}
	}



	/**
	 * @param dataSetMatrix the dataSetMatrix to set
	 */
	public void setDataSetMatrix() {
		int numRows = this.dataSet.size();
		int numCols = this.dataSet.get(0).getFeatureVector().size();
		this.dataSetMatrix = new int[numRows][numCols];
		for(int i=0;i<numRows;i++){
			for(int j=0;j<numCols;j++){
				this.dataSetMatrix[i][j] = this.dataSet.get(i).getFeatureVector().get(j);
			}
		}
	}
	
	/**
	 * @param dataSetMatrix the dataSetMatrix to set
	 */
	public void setDataSetMatrix(ImageSet images) {
		int numRows = 0;
		Iterator<Image> itr = images.getImages().iterator();
		while(itr.hasNext()){
			numRows += itr.next().getDescriptors().size();
		}
		int numCols = images.getImages().get(0).getDescriptors().get(0).getFeatureVector().size();
		this.dataSetMatrix = new int[numRows][numCols];
		Iterator<Image> imgitr = images.getImages().iterator();
		int fvcount = 0;
		while(imgitr.hasNext()){
			for(int rowcount=0;rowcount<imgitr.next().getDescriptors().size();rowcount++){
				for(int colcount=0;colcount<numCols;colcount++){
					this.dataSetMatrix[fvcount++][colcount] = imgitr.next().getDescriptors().get(rowcount).getFeatureVector().get(colcount);
				}
			}
		}
	}



	/**
	 * @return the dataSetMatrix
	 */
	public int[][] getDataSetMatrix() {
		return dataSetMatrix;
	}
}
